package com.springboot.springbootlogindemo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.springbootlogindemo.domain.MusicCollection;
import com.springboot.springbootlogindemo.domain.Song;
import com.springboot.springbootlogindemo.domain.User;
import com.springboot.springbootlogindemo.mapper.MusicCollectionMapper;
import com.springboot.springbootlogindemo.mapper.SongMapper;
import com.springboot.springbootlogindemo.mapper.UserMapper;
import com.springboot.springbootlogindemo.service.MusicCollectionService;
import com.springboot.springbootlogindemo.service.UserService;
import com.springboot.springbootlogindemo.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MusicCollectionServiceImpl extends ServiceImpl<MusicCollectionMapper, MusicCollection> implements MusicCollectionService {

    @Resource
    private MusicCollectionMapper musicCollectionMapper;

    @Resource
    private SongMapper songMapper;


    /**
     * 添加歌曲
     * @param songId
     * @param songListId
     * @return
     */
    @Override
    public Result addSong(Long songId, Long songListId) {
        MusicCollection collection = query().eq("song_id", songId).eq("song_list_id", songListId).one();
        if(collection!=null){
            return Result.error("歌单中已存在该歌曲");
        }
        return musicCollectionMapper.insert(new MusicCollection(songId,songListId))>0?Result.success("收藏成功"):Result.error("未知错误");
    }

    /**
     * 删除歌曲
     * @param songId
     * @param songListId
     * @return
     */
    @Override
    public Result deleteSong(Long songId, Long songListId) {
        MusicCollection collection = query().eq("song_id", songId).eq("song_list_id", songListId).one();
        if(collection==null){
            return Result.error("歌单中不存在该歌曲");
        }
        return musicCollectionMapper.deleteById(collection.getId())>0?Result.success("取消收藏成功"):Result.error("未知错误");
    }


    /**
     * 获取歌单所有歌曲
     * 那这里也可以做缓存，查到了之后，可以将这些歌放入缓存中自己歌单的名下， 然后下次查的时候先从缓存中查
     * @param songListId
     * @return
     */
    @Override
    public Result showSongs(Long songListId) {
        //双表联合查询
        List<Song> songsFromSongList = musicCollectionMapper.findSongsFromSongList(songListId);
        return Result.success(songsFromSongList);
    }

}
